package com.xinzhidi.web.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinzhidi.common.constant.AgreementConstants;
import com.xinzhidi.web.dtuusr.domain.DtuUsr;
import com.xinzhidi.web.dtuusr.service.DtuUsrService;
import com.xinzhidi.web.equipment.domain.Equipment;
import com.xinzhidi.web.equipment.service.EquipmentService;
import com.xinzhidi.web.netty.server.WsServer;
import com.xinzhidi.web.netty.service.DeviceInstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 自动抄表定时任务
 *
 * @author cx
 */
@Component("automaticMeterReadTask")
public class AutomaticMeterReadTask {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private DeviceInstructionService deviceInstructionService;

    @Autowired
    private DtuUsrService dtuUsrService;

    public void ryMultipleParams(Integer plotId) {
        System.out.println("==============小区id：【" + plotId + "】自动抄表定时任务执行");
        Integer port = WsServer.port;
        List<DtuUsr> dtuUsrList = dtuUsrService.list(new LambdaQueryWrapper<>(DtuUsr.class)
                .eq(DtuUsr::getPort, port));
        if (CollectionUtils.isEmpty(dtuUsrList)) {
            return;
        }

        //查询在线设备
        List<Equipment> list = equipmentService.list(new LambdaQueryWrapper<>(Equipment.class)
                .eq(Equipment::getLotId, plotId)
                .eq(Equipment::getAutoRead, "1")
                .ne(Equipment::getProtocolType, AgreementConstants.AgreementType.GREENHOUSE_COLLECTOR)
//                 .eq(Equipment::getStatus, 1)
                .eq(Equipment::getDelFlag, AgreementConstants.DEL_FLAG_NO)
                .in(Equipment::getDtuId, dtuUsrList.stream().map(DtuUsr::getId).collect(Collectors.toList())));

        //查询离线设备，并且离线设备没有还需要重连的指令


//        List<Long> dtuList = Lists.newArrayList();
//        List<String> addressList = Lists.newArrayList();
//        //获取待下发的需重连设备的地址
//        List<DeviceInstruction> onlineList = equipmentService.getReconnectAddress();
//        if(!org.springframework.util.CollectionUtils.isEmpty(onlineList)){
//            dtuList = onlineList.stream().map(DeviceInstruction::getDtuId).collect(Collectors.toList());
//            addressList = onlineList.stream().map(DeviceInstruction::getAddress).collect(Collectors.toList());
//        }
//        List<Equipment> equipmentList = equipmentService.list(new UpdateWrapper<Equipment>()
//                .notIn(!org.springframework.util.CollectionUtils.isEmpty(dtuList) ,"dtu_id", dtuList)
//                .notIn(!org.springframework.util.CollectionUtils.isEmpty(addressList) ,"address", addressList)
//                .eq("del_flag", 0)
//                .eq("status", 2));


        if (!CollectionUtils.isEmpty(list)) {
            //组装指令
            deviceInstructionService.assembleMessage(list, 3, AgreementConstants.InstructBusinessType.AUTO_METER_READ);
        }
        list = list.stream().filter(equipment -> equipment.getAddress().equals("83610643000000")).collect(Collectors.toList());
        System.out.println(list.size());
        //        if(!CollectionUtils.isEmpty(equipmentList)){
//            deviceInstructionService.assembleMessage(equipmentList , 3 , AgreementConstants.InstructBusinessType.AUTO_METER_READ);
//        }

    }

}
